/*
 * Created on Nov 11, 2003
 */

package com.unash.server;

/*
 * @author Ashwini Gokhale
 */
import java.util.ArrayList;
import java.util.List;

import com.unash.util.Consts;
 
public class ThreadPool implements Consts{
	
	final Channel workQueue;
	List<Thread> listOfWorkers;
	
	public void execute(Runnable r){
		try{
			workQueue.put(r);
		}//try
		catch(InterruptedException ie){
			Thread.currentThread().interrupt();
		}//catch
	}//execute 
	
	public ThreadPool(Channel ch, int nWorkers){
		listOfWorkers = new ArrayList<Thread>();
		workQueue = ch;
		for(int i = ZERO; i < nWorkers; ++i)
			activate();
	}//ThreadPool(Channel ch, int nWorkers)
	
	protected void activate(){
		Runnable runLoop = new Runnable(){
			public void run(){
				try{
					for(;;){
						Runnable r = (Runnable)(workQueue.take());
						r.run();
					}//for	
				}//try
				catch(InterruptedException ie){} //die
			}//run
		};//Runnable
		Thread myThread = new Thread(runLoop);
		myThread.start();
		listOfWorkers.add(myThread);
		//new Thread(runLoop).start();
	}//activate
}//ThreadPool